﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager
{
    private static GameManager mInstance;
    private List<TextScript> mTextList;
    private List<TextScript> mRecordedWrongTextList;
    private List<TextScript> mWrongTextList;
    public Player Player1;
    public Player Player2;
    private bool bIsFailed;
    public static GameManager GetInstance()
    {
        if (mInstance == null)
        {
            mInstance = new GameManager();
            mInstance.mTextList = new List<TextScript>();
            mInstance.mRecordedWrongTextList = new List<TextScript>();
            mInstance.mWrongTextList = new List<TextScript>();
        }
        return mInstance;
    }
    private float mGameStartTime;
    private int mGameScore;
    
    public void GameStart()
    {
        mGameStartTime = Time.time;
        mGameScore = 100;
        Player1 = GameObject.Find("player1").GetComponent<Player>();
        Player2 = GameObject.Find("player2").GetComponent<Player>();
        mTextList.Clear();
        mRecordedWrongTextList.Clear();
        mWrongTextList.Clear();
        bIsFailed = false;
    }
    public void TextDrop(TextScript script)
    {
        if (!script.IsWrongText)
        {
            mGameScore -= 10;
            mTextList.Remove(script);
            //Debug.Log("Current Score:" + mGameScore);
        }
        else
        {
            mWrongTextList.Remove(script);
        }
        if (mTextList.Count == 0)
        {
            ShowFailResult();
        }
        CheckAllTextMissded();
    }
    public void AddText(TextScript script)
    {
        mTextList.Add(script);
    }
    public void AddWroneText(TextScript script)
    {
        mWrongTextList.Add(script);
    }
    public TextScript GetRandomText()
    {
        if (mTextList.Count > 0)
        {
            return mTextList[0];
        }
        return null;
    }
    public void AddTextForce(Vector2 force, Vector2 currentVelocity)
    {
        foreach(var text in mTextList)
        {
            Rigidbody2D textRb = text.GetComponent<Rigidbody2D>();
            float velocityX = Mathf.Abs(textRb.velocity.x);
            if (text.IsBoarded() && velocityX < Mathf.Abs(currentVelocity.x))
            {
                textRb.AddForce(force);
            }
        }
        foreach (var text in mWrongTextList)
        {
            Rigidbody2D textRb = text.GetComponent<Rigidbody2D>();
            float velocityX = Mathf.Abs(textRb.velocity.x);
            if (text.IsBoarded() && velocityX < Mathf.Abs(currentVelocity.x))
            {
                textRb.AddForce(force);
            }
        }
    }
    public void AddRecordedWrongText(TextScript script)
    {
        mRecordedWrongTextList.Add(script);
    }
    public TextScript StoleAText()
    {
        if (mTextList.Count > 0)
        {
            TextScript script = mTextList[0];
            mTextList.RemoveAt(0);
            if (mTextList.Count == 0)
            {
                ShowFailResult();
            }
            CheckAllTextMissded();
            return script;
        }
        return null;
    }
    public void CheckAllTextMissded() //有bug 先用这个保护
    {
        bool allDisable = true;
        foreach (var script in mTextList)
        {
            if (script.gameObject.activeSelf || script.IsRecoreded)
            {
                allDisable = false;
                break;
            }
        }
        if (allDisable)
        {
            ShowFailResult();
        }
    }
    public void ShowResult()
    {
        Debug.Log("Total: 40 Remain:" + mTextList.Count + " Wrong: " + mRecordedWrongTextList.Count + " Use time:" + (Time.time - mGameStartTime));
        GameApp.GetInstance().TryEnd();
    }
    public void ShowFailResult() //全部掉光
    {
        Debug.Log("All Message Missed!");
        bIsFailed = true;
        GameApp.GetInstance().TryEnd();
    }
    public bool IsFailed()
    {
        return bIsFailed;
    }
    public int GetMissCount()
    {
        int missCount = 40 - mTextList.Count;
        float missPercent = (float)missCount / 40f;
        missCount = Mathf.Clamp(missCount, 0, 16)
;       return missCount;
    }
    public int GetMessCount()
    {
        int missCount = 40 - mTextList.Count;
        float missPercent = (float)missCount / 40f;
        if (missCount > 8)
        {
            missCount = missCount - 8;
        }
;       return missCount;
    }
    public int GetChaosCount()
    {
        int missCount = 40 - mTextList.Count;
        float missPercent = (float)missCount / 40f;
        if (missCount > 18)
        {
            missCount = missCount - 18;
        }
;       return missCount;
    }
    public int GetExtraCount()
    {
        return mRecordedWrongTextList.Count;
    }
}
